home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmiSoft / Comm / tcp / JabberwockySRC.lha / Jabberwocky / src / groupchat.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-03-29  |  11.7 KB  |  438 lines

  1. /*  Copyright (C) 2002 Tom Parker (tom@carrott.org),
  2.                        Matthias Münch (matthias@amigaworld.de)
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17.  
  18.     In addition, as a special exception, Tom Parker and Matthias Münch give
  19.     permission to link the code of this program with a TCP stack of your 
  20.     choice, any official MUI libraries or classes and any custom MUI classes
  21.     that should be necessary for the operation of this program.  This 
  22.     exception also gives you permission to distribute linked combinations 
  23.     including this software with any of the before-mentioned libraries and 
  24.     classes.  You must obey the GNU General Public License in all respects for
  25.     all of the code used other than that provided by the before-mentioned 
  26.     libraries and classes.  As part of this exception you are obliged to 
  27.     follow the license terms of the before-mentioned libraries, this license
  28.     does not compel you to follow those terms, but if you do not then you may
  29.     not link with those libraries.  If you modify this file, you may extend
  30.     this exception to your version of the file, but you are not obligated to
  31.     do so.  If you do not wish to do so, delete this exception statement from
  32.     your version.
  33. */
  34. /*
  35. ** group chat window
  36. */
  37.  
  38. #include "common.h"
  39. #include "edit.h"
  40.  
  41. #include <MUI/NListview_mcc.h>
  42.  
  43. static list *gc_list = NULL;
  44.  
  45. static ULONG gchat_new(struct IClass *cl, Object *obj, struct opSet *msg);
  46. static MUI_LIST_DISP_DECL(userlist_disp, struct gc_user *u);
  47. static MUI_LIST_DEST_DECL(userlist_dest, struct gc_user *u);
  48. static MUI_LIST_COMP_DECL(userlist_comp, struct gc_user *u1, struct gc_user *u2);
  49. static void gchat_send(Object *obj, struct gchatdata *data);
  50. static void gchat_presence(struct gchatdata *data, ikspak *pak);
  51. static void gchat_message(struct gchatdata *data, ikspak *pak);
  52.  
  53. static void gchat_people(struct gchatdata *data, int diff)
  54. {
  55.     if(!data->gcw) return;
  56.  
  57.     data->gcw->people += diff;
  58.     if(data->gcw->people == 0)
  59.         set(data->numtxt, MUIA_Text_Contents, (ULONG) "-empty-");
  60.     else
  61.         set(data->numtxt, MUIA_Text_Contents, (ULONG) my_printf("%ld people", data->gcw->people));
  62. }
  63.  
  64. static void gchat_nick(struct gchatdata *data, char *nick)
  65. {
  66.     if(!nick || !*nick) return;
  67.  
  68.     if(data->nick) free(data->nick);
  69.     data->nick = strdup(nick);
  70.  
  71.     DoMethod(data->rect, MUIM_Group_InitChange);
  72.     set(data->nicktxt, MUIA_Text_Contents, (ULONG) data->nick);
  73.     DoMethod(data->rect, MUIM_Group_ExitChange);
  74. }
  75.  
  76.  
  77. MUI_DISPATCH(gchat_dispatch)
  78. {
  79.     switch(msg->MethodID)
  80.     {
  81.     case OM_NEW:
  82.         return gchat_new(cl, obj, (APTR)msg);
  83.  
  84.     case GCHAT_CLOSE:
  85.     {
  86.         struct gchatdata *data = INST_DATA(cl,obj);
  87.         list_remove(&gc_list, data->gcw);
  88.         if(data->gcw->id)
  89.         {
  90.             iks *x;
  91.             x = iks_make_pres(IKS_TYPE_UNAVAILABLE, 0, iks_id_print(data->gcw->id), NULL);
  92.             iks_send(net.parser, x);
  93.             iks_delete(x);
  94.         }
  95.         free(data->gcw);
  96.         DoMethod(gui.app, MUIM_Application_PushMethod, gui.list, 2, ROSTER_CLOSEME, obj);
  97.         return 0;
  98.     }
  99.  
  100.     case GCHAT_TOPIC:
  101.     {
  102.         struct gchatdata *data = INST_DATA(cl,obj);
  103.         iks *x;
  104.         char *tmp;
  105.  
  106.         tmp = mui_sget(data->topicstr);
  107.         if(tmp)
  108.         {
  109.             x = iks_make_msg(IKS_TYPE_GROUPCHAT, iks_id_print(data->gcw->id), NULL, NULL);
  110.             iks_insert_cdata(iks_insert(x, "subject"), convert_utf8(tmp), -1);
  111.             iks_send(net.parser, x);
  112.             iks_delete(x);
  113.         }
  114.         set(obj, MUIA_Window_ActiveObject, (ULONG) data->msgstr);
  115.         return 0;
  116.     }
  117.  
  118.     case GCHAT_SEND:
  119.         gchat_send(obj, INST_DATA(cl,obj));
  120.         return 0;
  121.  
  122.     case GCHAT_PACKET:
  123.     {
  124.         struct gchatdata *data = INST_DATA(cl,obj);
  125.         ikspak *pak = (ikspak *)MARG1;
  126.  
  127.         if(pak->type == IKS_PAK_PRESENCE)
  128.             gchat_presence(data, pak);
  129.         else
  130.             gchat_message(data, pak);
  131.  
  132.         return 0;
  133.     }
  134.  
  135.     }
  136.     return DoSuperMethodA(cl, obj, msg);
  137. }
  138.  
  139.  
  140. static ULONG gchat_new(struct IClass *cl, Object *obj, struct opSet *msg)
  141. {
  142.     static struct Hook dispHook = { {0,0}, &userlist_disp, NULL, NULL };
  143.     static struct Hook destHook = { {0,0}, &userlist_dest, NULL, NULL };
  144.     static struct Hook compHook = { {0,0}, &userlist_comp, NULL, NULL };
  145.     struct gchatdata *data;
  146.     struct gc_win *gcw;
  147.     Object *chantxt, *topicstr, *msglist, *userlist, *numtxt, *rect, *nicktxt, *msgstr;
  148.  
  149.     obj = (Object *)DoSuperNew(cl, obj,
  150.         MUIA_Window_Title, "Jabber Conference",
  151.         MUIA_Window_ID, mui_id(gui.app, "GCH"),
  152.         MUIA_HelpNode, "window-conference",
  153.         WindowContents, VGroup,
  154.             Child, (ULONG) chantxt = TextObject,
  155.                 MUIA_Text_PreParse, "\33c",
  156.             End,
  157.             Child, HGroup,
  158.                 Child, Label2(MSG_GCHAT_TOPIC),
  159.                 Child, (ULONG) topicstr = TextinputObject,
  160.                     StringFrame,
  161.                     MUIA_String_MaxLen, 256,
  162.                     MUIA_CycleChain, 1,
  163.                 End,
  164.             End,
  165.             Child, HGroup,
  166.                 Child, (ULONG) msglist = NewObject(gui.chatarea_mcc->mcc_Class, NULL,
  167.                     MUIA_HorizWeight, 75,
  168.                 TAG_DONE),
  169.                 Child, BalanceObject, End,
  170.                 Child, VGroup,
  171.                     MUIA_HorizWeight, 25,
  172.                     Child, (ULONG) numtxt = TextObject,
  173.                         MUIA_Text_PreParse, "\33c",
  174.                     End,
  175.                     Child, NListviewObject,
  176.                         MUIA_NListview_NList, (ULONG) userlist = NListObject,
  177.                             InputListFrame,
  178.                             MUIA_NList_DisplayHook, &dispHook,
  179.                             MUIA_NList_DestructHook, &destHook,
  180.                             MUIA_NList_CompareHook, &compHook,
  181.                             MUIA_NList_DefaultObjectOnClick, FALSE,
  182.                             MUIA_NList_Format, "",
  183.                             MUIA_CycleChain, 1,
  184.                         End,
  185.                     End,
  186.                 End,
  187.             End,
  188.             Child, (ULONG) rect = HGroup,
  189.                 Child, (ULONG) nicktxt = TextObject,
  190.                     MUIA_HorizWeight, 0,
  191.                 End,
  192.                 Child, (ULONG) msgstr = NewObject(gui.edit_mcc->mcc_Class, NULL,
  193.                     EDIT_TYPE, EDIT_TYPE_CHAT,
  194.                     MUIA_CycleChain, 1,
  195.                 TAG_DONE),
  196.             End,
  197.         End,
  198.         TAG_MORE, msg->ops_AttrList);
  199.  
  200.     if(!obj) return(NULL);
  201.  
  202.     set(obj, MUIA_Window_DefaultObject, (ULONG) msgstr);
  203.     set(obj, MUIA_Window_ActiveObject, (ULONG) msgstr);
  204.  
  205.     gcw = malloc(sizeof(struct gc_win));
  206.     memset(gcw, 0, sizeof(struct gc_win));
  207.     gcw->win = obj;
  208.     list_append(&gc_list, gcw);
  209.  
  210.     data = INST_DATA(cl,obj);
  211.     data->chantxt = chantxt;
  212.     data->topicstr = topicstr;
  213.     data->msglist = msglist;
  214.     data->userlist = userlist;
  215.     data->numtxt = numtxt;
  216.     data->rect = rect;
  217.     data->nicktxt = nicktxt;
  218.     data->msgstr = msgstr;
  219.     data->gcw = gcw;
  220.     data->nick = NULL;
  221.     gcw->data = data;
  222.  
  223.     DoMethod(topicstr, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime, obj, 1, GCHAT_TOPIC);
  224.     DoMethod(msgstr, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime, obj, 1, GCHAT_SEND);
  225.     DoMethod(obj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, obj, 1, GCHAT_CLOSE);
  226.  
  227.     gchat_people(data, 0);
  228.  
  229.     if(net.id)
  230.         gchat_nick(data, net.id->user);
  231.     else
  232.         gchat_nick(data, "jwocky");
  233.  
  234.     return((ULONG)obj);
  235. }
  236.  
  237.  
  238. MUI_LIST_DISP_STATIC(userlist_disp, struct gc_user *u)
  239. {
  240.     *array = u->nick;
  241.     return 0;
  242. }
  243.  
  244.  
  245. MUI_LIST_DEST_STATIC(userlist_dest, struct gc_user *u)
  246. {
  247.     gchat_people(u->gcw->data, -1);
  248.  
  249.     list_remove(&u->gcw->users, u);
  250.     if(u->nick) free(u->nick);
  251.     free(u);
  252.  
  253.     return 0;
  254. }
  255.  
  256.  
  257. MUI_LIST_COMP_STATIC(userlist_comp, struct gc_user *u1, struct gc_user *u2)
  258. {
  259.     return iks_strcmp(u1->nick, u2->nick);
  260. }
  261.  
  262.  
  263. static void gchat_send(Object *obj, struct gchatdata *data)
  264. {
  265.     char *tmp;
  266.     iks *x;
  267.  
  268.     tmp = mui_sget(data->msgstr);
  269.     if(!tmp) return;
  270.  
  271.     if(tmp[0] == '/')
  272.     {
  273.         if(strnicmp("join ", tmp+1, 5) == 0)
  274.         {
  275.             if(data->gcw->id)
  276.             {
  277.                 DoMethod(data->msglist, CHATAREA_ADD, 2, "***", "Already joined.");
  278.                 goto out;
  279.             }
  280.             DoMethod(data->msglist, CHATAREA_ADD, 2, "***", "Joining...");
  281.             data->gcw->id = iks_id_new(NULL, tmp+6);
  282.             if(!data->gcw->id->resource) data->gcw->id->resource = data->nick;
  283. /*            data->gcw->nick = strdup(data->gcw->id->resource);
  284. */            x = iks_make_pres(0, 0, iks_id_print(data->gcw->id), NULL);
  285.             iks_send(net.parser, x);
  286.             iks_delete(x);
  287.             data->gcw->id->resource = NULL;
  288.             data->gcw->id->full = NULL; /* FIX ME */
  289.             set(data->chantxt, MUIA_Text_Contents, (ULONG) iks_id_print(data->gcw->id));
  290.         }
  291.         else if(strnicmp("part", tmp+1, 4) == 0)
  292.         {
  293.             if(!data->gcw->id) goto out;
  294.             x = iks_make_pres(IKS_TYPE_UNAVAILABLE, 0, iks_id_print(data->gcw->id), NULL);
  295.             iks_send(net.parser, x);
  296.             iks_delete(x);
  297.             data->gcw->id = NULL;
  298.             set(data->chantxt, MUIA_Text_Contents, (ULONG) "");
  299.             DoMethod(data->msglist, CHATAREA_ADD, 2, "***", "Channel left.");
  300.             DoMethod(data->userlist, MUIM_NList_Clear);
  301.         }
  302.         else if(strnicmp("me ", tmp+1, 3) == 0)
  303.         {
  304.             if(!data->gcw->id)
  305.             {
  306.                 DoMethod(data->msglist, CHATAREA_ADD, 2, "***", "Not joined.");
  307.                 goto out;
  308.             }
  309.             x = iks_make_msg(IKS_TYPE_GROUPCHAT, iks_id_print(data->gcw->id), convert_utf8(tmp), NULL);
  310.             iks_send(net.parser, x);
  311.             iks_delete(x);
  312.         }
  313.         else
  314.         {
  315.             DoMethod(data->msglist, CHATAREA_ADD, 2, "***", "Unknown command.");
  316.         }
  317.     }
  318.     else
  319.     {
  320.         if(!data->gcw->id)
  321.         {
  322.             DoMethod(data->msglist, CHATAREA_ADD, 2, "***", "Not joined.");
  323.             goto out;
  324.         }
  325.         x = iks_make_msg(IKS_TYPE_GROUPCHAT, iks_id_print(data->gcw->id), convert_utf8(tmp), NULL);
  326.         iks_send(net.parser, x);
  327.         iks_delete(x);
  328.     }
  329.  
  330.  
  331. out:
  332.     set(data->msgstr, MUIA_String_Contents, NULL);
  333. }
  334.  
  335.  
  336. static struct gc_user *gchat_finduser(struct gc_win *gcw, char *nick)
  337. {
  338.     struct gc_user *u;
  339.  
  340.     if(!gcw->users) return(NULL);
  341.     u = list_first(&gcw->users);
  342.     while(u)
  343.     {
  344.         if(iks_strcmp(nick, u->nick) == 0) return(u);
  345.         u = list_next(u);
  346.     }
  347.     return NULL;
  348. }
  349.  
  350.  
  351. static void gchat_presence(struct gchatdata *data, ikspak *pak)
  352. {
  353.     struct gc_user *u;
  354.  
  355.     u = gchat_finduser(data->gcw, pak->from->resource);
  356.     if(u)
  357.     {
  358.         long pos = MUIV_NList_GetPos_Start;
  359.         if(pak->show != IKS_SHOW_UNAVAILABLE) return;
  360.         DoMethod(data->userlist, MUIM_NList_GetPos, u->nick, &pos);
  361.         if(data->gcw->state > 0) DoMethod(data->msglist, CHATAREA_ADD, 2, "<--", my_printf("%s has left.", u->nick));
  362.         if(pos != MUIV_NList_GetPos_End) DoMethod(data->userlist, MUIM_NList_Remove, pos);
  363.     }
  364.     else
  365.     {
  366.         if(pak->show == IKS_SHOW_UNAVAILABLE) return;
  367.         u = malloc(sizeof(struct gc_user));
  368.         if(!u) return;
  369.         u->nick = iks_strdup(pak->from->resource);
  370.         u->gcw = data->gcw;
  371.         list_append(&data->gcw->users, u);
  372.         DoMethod(data->userlist, MUIM_NList_InsertSingle, u, MUIV_NList_Insert_Sorted);
  373.         gchat_people(data, 1);
  374.         if(data->gcw->state > 0) DoMethod(data->msglist, CHATAREA_ADD, 2, "-->", my_printf("%s has joined.", u->nick));
  375.         if(data->gcw->state == 0 && iks_strcmp(data->nick, pak->from->resource) == 0)
  376.         {
  377.             DoMethod(data->msglist, CHATAREA_ADD, 2, "***", "Joined.");
  378.             data->gcw->state = 1;
  379.         }
  380.     }
  381.  
  382.     return;
  383. }
  384.  
  385.  
  386. static void gchat_message(struct gchatdata *data, ikspak *pak)
  387. {
  388.     char *body, *topic;
  389.  
  390.     body = iks_find_cdata(pak->x, "body");
  391.     topic = iks_find_cdata(pak->x, "subject");
  392.  
  393.     if(topic)
  394.         set(data->topicstr, MUIA_String_Contents, (ULONG) convert_locale(topic));
  395.  
  396.     if(pak->from->resource)
  397.     {
  398.         if(strnicmp("/me ", body, 4) == 0)
  399.             DoMethod(data->msglist, CHATAREA_ADD, 0, "\33r\33b*", my_printf("\33b%s\33n %s", pak->from->resource, body+4));
  400.         else
  401.             DoMethod(data->msglist, CHATAREA_ADD, 0, pak->from->resource, body);
  402.     }
  403. }
  404.  
  405.  
  406. void gchat_on(char *chan)
  407. {
  408.     Object *win;
  409.  
  410.     win = NewObject(gui.gchat_mcc->mcc_Class, NULL, TAG_DONE);
  411.     if(win)
  412.     {
  413.         DoMethod(gui.app, OM_ADDMEMBER, win);
  414.         set(win, MUIA_Window_Open, TRUE);
  415.     }
  416. }
  417.  
  418.  
  419. int gchat_packet(ikspak *pak)
  420. {
  421.     struct gc_win *gcw;
  422.     char *tmp;
  423.  
  424.     if(!gc_list) return(0);
  425.  
  426.     gcw = list_first(&gc_list);
  427.     while(gcw)
  428.     {
  429.         if(iks_id_cmpx(pak->from, gcw->id, IKS_ID_USER | IKS_ID_SERVER) == 0)
  430.         {
  431.             DoMethod(gcw->win, GCHAT_PACKET, pak);
  432.             return 1;
  433.         }
  434.         gcw = list_next(gcw);
  435.     }
  436.     return 0;
  437. }
  438.